using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UMC.Data;
using UMC.WorkFlow.Subassembly;

namespace UMC.WorkFlow
{
    /// <summary>
    /// 工作流中的基本身份
    /// </summary>

    [UMC.WorkFlow.DefaultDesign(ArgumentType = UMC.WorkFlow.ArgumentType.Command, ArgumentValue = "IdentitySetting")]
    public class IdentitySetting : UMC.Data.IJSONType
    {

        [Design(Caption = "用脚本返回审批人", Explain = "第一列的数据做为审批人")]
        public SqlSetting SQL { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [Design(Caption = "用户名")]
        public string Username { get; set; }
        protected virtual string JSONName()
        {
            return this.GetType().Name;
        }
        public virtual Func<object> GetInstance(string prototypeName)
        {
            return () => String.Empty;
            //throw new NotImplementedException();
        }

        public virtual void Read(string key, object value)
        {
            switch (key)
            {
                case "Username":
                    this.Username = value as string;
                    break;
                case "SQL":
                    this.SQL = value as SqlSetting;
                    break;
            }
        }
        protected virtual void WriteProperty(TextWriter writer)
        {
            if (String.IsNullOrEmpty(this.Username) == false)
            {
                writer.Write(',');
                JSON.Serialize("Username", this.Username, writer);
            }
            if (this.SQL != null)
            {
                writer.Write(',');

                JSON.Serialize("SQL", this.SQL, writer);
            }
        }

        public void Write(TextWriter writer)
        {
            writer.Write('{');
            JSON.Serialize(JSON.Constructor, JSONName(), writer);
            WriteProperty(writer);
            writer.Write('}');
        }
    }
}
